package com.neuedu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.common.ServerResponse;
import com.neuedu.entity.Schedule;
import com.neuedu.service.ScheduleService;
import com.neuedu.vo.ScheduleCondition;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("医生排班管理")
@RestController
@RequestMapping("/schedule")
@CrossOrigin
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;

    @ApiOperation("医生排班列表")
    @GetMapping("/list")
    public ServerResponse list(ScheduleCondition scheduleCondition) {

        //调用业务方法
        Page<Schedule> schedulePage = scheduleService.getScheduleList(scheduleCondition);

        //返回分页对象
        return ServerResponse.success("").data("schedulePage", schedulePage);
    }

    @ApiOperation("添加医生排班")
    @PostMapping("/add")
    public ServerResponse add(@RequestBody Schedule schedule) {

        //服务器端数据校验---医生排班名称
        if(StringUtils.isEmpty(schedule.getScheduleName()))  {
            return ServerResponse.error("医生排班名称不能为空");
        }

        if(schedule.getScheduleName().length()>50){
            return ServerResponse.error("医生排班名称的长度不能超过50个字符");
        }

        //添加操作
        if(scheduleService.save(schedule)){
            return ServerResponse.success("添加医生排班成功");
        }else{
            return ServerResponse.error("添加医生排班失败");
        }
    }

    @ApiOperation("根据编号获取医生排班")
    @GetMapping("/get/{scheduleId}")
    public ServerResponse get(@PathVariable int scheduleId) {

        Schedule schedule = scheduleService.getById(scheduleId);

        if(schedule!=null){
            return ServerResponse.success("").data("schedule", schedule);
        }else{
            return ServerResponse.error("获取医生排班信息失败");
        }
    }

    @ApiOperation("修改医生排班")
    @PutMapping("/modify")
    public ServerResponse modify(@RequestBody Schedule schedule) {

        //服务器端数据校验---医生排班名称
        if(StringUtils.isEmpty(schedule.getScheduleName()))  {
            return ServerResponse.error("医生排班名称不能为空");
        }

        if(schedule.getScheduleName().length()>50){
            return ServerResponse.error("医生排班名称的长度不能超过50个字符");
        }

        //修改操作
        if(scheduleService.updateById(schedule)){
            return ServerResponse.success("修改医生排班成功");
        }else{
            return ServerResponse.error("修改医生排班失败");
        }
    }

    @ApiOperation("删除医生排班")
    @DeleteMapping("/remove/{scheduleId}")
    public ServerResponse remove(@PathVariable int scheduleId) {

        if(scheduleService.removeById(scheduleId)){
            return ServerResponse.success("删除医生排班成功");
        }else{
            return ServerResponse.error("删除医生排班失败");
        }
    }

    @ApiOperation("判断指定的医生排班是否可以删除")
    @GetMapping("/removeEnable/{scheduleId}")
    public ServerResponse removeEnable(@PathVariable int scheduleId) {

        if(scheduleService.removeEnable(scheduleId)){
            return ServerResponse.success("允许删除医生排班");
        }else{
            return ServerResponse.error("当前医生排班下存在挂号，不能删除");
        }
    }

    @ApiOperation("批量删除医生排班")
    @DeleteMapping("/deleteBatch")
    public ServerResponse deleteBatch(String scheduleIds) {

        if(scheduleService.deleteBatch(scheduleIds)){
            return ServerResponse.success("批量删除医生排班成功");
        }else{
            return ServerResponse.error("批量删除医生排班成功失败");
        }
    }


    @ApiOperation("获取全部排班列表")
    @GetMapping("/listAll")
    public ServerResponse listAll() {

        List<Schedule> scheduleList = scheduleService.list();

        return ServerResponse.success("").data("scheduleList", scheduleList);
    }
}